import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { join } from 'path';
import { UsePrismaAutoDateMiddleware } from './common/middleware/prisma-auto-date.middleware';
import { PrismaModule } from 'nestjs-prisma';
import { ConfigModule, ConfigService } from '@nestjs/config';
import config from './config/config';
import { SecurityConfig } from './config/config.interface';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { ServeStaticModule } from '@nestjs/serve-static';
import { UserModule } from './core/user/user.module';
import { DictModule } from './core/dict/dict.module';
import { ModelModule } from './core/model/model.module';
import { GroupModule } from './core/group/group.module';
import { SizeModule } from './core/size/size.module';
import { ProcessModule } from './core/process/process.module';
import { WorkerModule } from './core/worker/worker.module';
import { RecordModule } from './core/record/record.module';
import { CaptchaService } from './common/utils/captcha/captcha.service';
import { JwtStrategy } from './common/strategy/jwt.strategy';
import { StyleModule } from './core/style/style.module';
import { ProcessTemplateModule } from './core/process-template/process-template.module';
import { ReportModule } from './core/report/report.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [config],
    }),
    JwtModule.registerAsync({
      global: true,
      inject: [ConfigService],
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => {
        const securityConfig = configService.get<SecurityConfig>('security');
        return {
          secret: configService.get('JWT_ACCESS_SECRET'),
          signOptions: {
            expiresIn: securityConfig.expiresIn,
          },
        };
      },
    }),
    PassportModule.register({ defaultStrategy: 'jwt' }),
    PrismaModule.forRoot({
      isGlobal: true,
      prismaServiceOptions: {
        middlewares: [UsePrismaAutoDateMiddleware()],
      },
    }),
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '../webapp/dist'),
    }),
    UserModule,
    DictModule,
    ModelModule,
    GroupModule,
    SizeModule,
    ProcessModule,
    WorkerModule,
    RecordModule,
    StyleModule,
    ProcessTemplateModule,
    ReportModule,
  ],
  controllers: [AppController],
  providers: [AppService, JwtStrategy, CaptchaService],
})
export class AppModule {}
